Mercurial > projects > doodle
annotate doodle/dia/tool_layer.d @ 60:e64baac3efb2
Attempt at a templatised palette
author | David Bryant <bagnose@gmail.com> |
---|---|
date | Tue, 10 Aug 2010 22:55:54 +0930 |
parents | c63719604adb |
children | 20d6327c4a75 |
rev | line source |
---|---|
28
1754cb773d41
Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents:
26
diff
changeset
|
1 module doodle.dia.tool_layer; |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
2 |
36 | 3 public { |
4 import doodle.dia.tool; | |
5 } | |
6 | |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
14
diff
changeset
|
7 private { |
28
1754cb773d41
Part-way through getting to compile with configure/builder.
Graham St Jack <graham.stjack@internode.on.net>
parents:
26
diff
changeset
|
8 import doodle.cairo.routines; |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
9 import doodle.core.logging; |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
10 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
11 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
12 // This interface is used by the palette |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
13 interface IToolStack { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
14 void use(Tool tool); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
15 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
16 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
17 interface IToolStackObserver { |
60
e64baac3efb2
Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
58
diff
changeset
|
18 void toolChanged(Tool tool); |
16
9e63308b749c
* Fix up public/private includes
David Bryant <daveb@acres.com.au>
parents:
14
diff
changeset
|
19 } |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
20 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
21 final class ToolLayer : Layer, IEventHandler, IToolStack { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
22 this(in Tool[] tools, IToolStackObserver observer, in string name = "Tool") { |
12 | 23 super(name); |
48 | 24 _tools = tools.dup; |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
25 _observer = observer; |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
26 } |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
27 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
28 // IToolStack overrides: |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
29 |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
30 void use(Tool tool) { |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
31 assert(_grabbedTool is null); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
32 message("using new tool: %s", tool.name); |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
33 _tools ~= tool; |
60
e64baac3efb2
Attempt at a templatised palette
David Bryant <bagnose@gmail.com>
parents:
58
diff
changeset
|
34 _observer.toolChanged(tool); |
7 | 35 } |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
36 |
36 | 37 // Layer overrides: |
38 | |
39 override Rectangle bounds() const { | |
40 return Rectangle(); | |
41 } | |
42 | |
57 | 43 override void draw(const IViewport viewport, |
48 | 44 in Rectangle pixelDamage, scope Context pixelCr, |
45 in Rectangle modelDamage, scope Context modelCr) const { | |
56 | 46 if (_grabbedTool) { |
47 _grabbedTool.draw(viewport, pixelDamage, pixelCr); | |
36 | 48 } |
49 } | |
50 | |
51 // EventHandler overrides: | |
52 | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
53 bool handleButtonPress(scope IViewport viewport, in ButtonEvent event) { |
14 | 54 // writefln("%s", event); |
7 | 55 |
48 | 56 if (_grabbedTool is null) { |
57 foreach_reverse(ref tool; _tools) { | |
58 if (tool.handleButtonPress(viewport, event)) { | |
59 _grabbedTool = &tool; | |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
60 _grabbedButton = event.buttonName; |
7 | 61 break; |
62 } | |
63 } | |
64 } | |
65 else { | |
48 | 66 _grabbedTool.handleButtonPress(viewport, event); |
7 | 67 } |
68 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
69 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
70 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
71 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
72 bool handleButtonRelease(scope IViewport viewport, in ButtonEvent event) { |
14 | 73 // writefln("%s", event); |
7 | 74 |
48 | 75 if (_grabbedTool !is null) { |
76 _grabbedTool.handleButtonRelease(viewport, event); | |
7 | 77 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
78 if (_grabbedButton == event.buttonName) { |
48 | 79 _grabbedTool = null; |
7 | 80 } |
81 } | |
82 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
83 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
84 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
85 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
86 bool handleKeyPress(scope IViewport viewport, in KeyEvent event) { |
14 | 87 // writefln("%s", event); |
88 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
89 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
90 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
91 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
92 bool handleKeyRelease(scope IViewport viewport, in KeyEvent event) { |
14 | 93 // writefln("%s", event); |
94 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
95 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
96 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
97 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
98 bool handleMotion(scope IViewport viewport, in MotionEvent event) { |
17 | 99 //writefln("%s", event); |
7 | 100 |
48 | 101 if (_grabbedTool is null) { |
102 foreach_reverse(ref tool; _tools) { | |
103 if (tool.handleMotion(viewport, event)) { | |
7 | 104 break; |
105 } | |
106 } | |
107 } | |
108 else { | |
48 | 109 _grabbedTool.handleMotion(viewport, event); |
7 | 110 } |
111 | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
112 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
113 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
114 |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
115 bool handleScroll(scope IViewport viewport, in ScrollEvent event) { |
14 | 116 // writefln("%s", event); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
117 |
48 | 118 if (_grabbedTool is null) { |
119 foreach_reverse(ref tool; _tools) { | |
120 if (tool.handleScroll(viewport, event)) { | |
7 | 121 break; |
122 } | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
123 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
124 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
125 else { |
48 | 126 _grabbedTool.handleScroll(viewport, event); |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
127 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
128 |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
129 return true; |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
130 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
131 |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
132 private { |
48 | 133 Tool[] _tools; |
58
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
134 IToolStackObserver _observer; |
c63719604adb
Beginnings of creating a rectangle...
"David Bryant <bagnose@gmail.com>"
parents:
57
diff
changeset
|
135 |
48 | 136 Tool * _grabbedTool; |
137 ButtonName _grabbedButton; | |
2
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
138 } |
d6f44347373d
* Switched over to geometry done with structs instead of classes.
David Bryant <daveb@acres.com.au>
parents:
diff
changeset
|
139 } |