summaryrefslogtreecommitdiff
path: root/examples/example_10_drawing.nim
blob: 9b342ba526663913e4dce0ab21632280bed482fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This example shows how to draw the surface of a control.

import nigui

app.init()
var window = newWindow()
window.width = 500
window.height = 500

var control1 = newControl()
window.add(control1)
# Creates a drawable control

control1.widthMode = WidthMode_Fill
control1.heightMode = HeightMode_Fill
# Let it fill out the whole window

var image1 = newImage()
image1.loadFromFile("example_01_basic_app.png")
# Reads the file and holds the image as bitmap in memory

var image2 = newImage()
image2.resize(2, 2)
# Creates a new bitmap

image2.canvas.setPixel(0, 0, rgb(255, 0, 0))
image2.canvas.setPixel(0, 1, rgb(255, 0, 0))
image2.canvas.setPixel(1, 1, rgb(0, 255, 0))
image2.canvas.setPixel(1, 0, rgb(0, 0, 255))
# Modifies single pixels

control1.onDraw = proc (event: DrawEvent) =
  let canvas = event.control.canvas
  # A shortcut

  canvas.areaColor = rgb(30, 30, 30) # dark grey
  canvas.fill()
  # Fill the whole area

  canvas.setPixel(0, 0, rgb(255, 0, 0))
  # Modifies a single pixel

  canvas.areaColor = rgb(255, 0, 0) # red
  canvas.drawRectArea(10, 10, 30, 30)
  # Draws a filled rectangle

  canvas.lineColor = rgb(255, 0, 0) # red
  canvas.drawLine(60, 10, 110, 40)
  # Draws a line

  let text = "Hello World!"
  canvas.textColor = rgb(0, 255, 0) # lime
  canvas.fontSize = 20
  canvas.fontFamily = "Arial"
  canvas.drawText(text, 10, 70)
  # Outputs a text

  canvas.drawRectOutline(10, 70, canvas.getTextWidth(text), canvas.getTextLineHeight())
  # Draws a rectangle outline

  canvas.drawImage(image1, 10, 120)
  # Draws an image in original size

  canvas.drawImage(image2, 120, 120, 50)
  # Draws an image stretched

control1.onMouseButtonDown = proc (event: MouseEvent) =
  echo(event.button, " (", event.x, ", ", event.y, ")")
  # Shows where the mouse is clicked in control-relative coordinates

window.show()
app.run()