binary.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // binary.cs
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. namespace Samples {
  35. public partial class binary: Form {
  36. private Region r1;
  37. private Region r2;
  38. private Region op;
  39. private Graphics gfx;
  40. public binary ()
  41. {
  42. InitializeComponent ();
  43. object[] shapes = Samples.Common.Shapes.GetList ();
  44. shape1ComboBox.Items.AddRange (shapes);
  45. shape2ComboBox.Items.AddRange (shapes);
  46. object[] ops = new object[] {
  47. "Union",
  48. "Intersect",
  49. "Exclude",
  50. "Complement",
  51. "Xor"
  52. };
  53. shape3comboBox.Items.AddRange (ops);
  54. object[] matrices = Samples.Common.Matrices.GetList ();
  55. matrix1comboBox.Items.AddRange (matrices);
  56. matrix2comboBox.Items.AddRange (matrices);
  57. matrix3comboBox.Items.AddRange (matrices);
  58. }
  59. private void Form1_Paint (object sender, PaintEventArgs e)
  60. {
  61. gfx = e.Graphics;
  62. if (shape1checkBox.Checked && (r1 != null))
  63. gfx.FillRegion (Brushes.Red, r1);
  64. if (shape2checkBox.Checked && (r2 != null))
  65. gfx.FillRegion (Brushes.Green, r2);
  66. if (shape3checkBox.Checked && (op != null))
  67. gfx.FillRegion (Brushes.Blue, op);
  68. }
  69. private void resetButton_Click (object sender, EventArgs e)
  70. {
  71. shape1ComboBox.SelectedIndex = -1;
  72. if (r1 != null) {
  73. r1.Dispose ();
  74. r1 = null;
  75. }
  76. shape2ComboBox.SelectedIndex = -1;
  77. if (r2 != null) {
  78. r2.Dispose ();
  79. r2 = null;
  80. }
  81. if (op != null) {
  82. op.Dispose ();
  83. op = null;
  84. }
  85. UpdateUI ();
  86. }
  87. private void UpdateUI ()
  88. {
  89. Invalidate ();
  90. Update ();
  91. }
  92. private GraphicsPath GetShape (ComboBox cb)
  93. {
  94. return Samples.Common.Shapes.GetShape (cb.SelectedIndex);
  95. }
  96. private void UpdateShape1 ()
  97. {
  98. GraphicsPath path = GetShape (shape1ComboBox);
  99. if (path != null) {
  100. r1 = new Region (path);
  101. path.Dispose ();
  102. }
  103. }
  104. private void shape1_Changed (object sender, EventArgs e)
  105. {
  106. UpdateShape1 ();
  107. if (op != null) {
  108. op.Dispose ();
  109. op = null;
  110. }
  111. UpdateUI ();
  112. }
  113. private void UpdateShape2 ()
  114. {
  115. GraphicsPath path = GetShape (shape2ComboBox);
  116. if (path != null) {
  117. r2 = new Region (path);
  118. path.Dispose ();
  119. }
  120. }
  121. private void shape2_Changed (object sender, EventArgs e)
  122. {
  123. UpdateShape2 ();
  124. if (op != null) {
  125. op.Dispose ();
  126. op = null;
  127. }
  128. UpdateUI ();
  129. }
  130. private void UpdateShape3 ()
  131. {
  132. if (shape3comboBox.SelectedIndex == -1)
  133. return;
  134. if (r1 != null) {
  135. if (op != null)
  136. op.Dispose ();
  137. op = r1.Clone ();
  138. if (r2 != null) {
  139. switch (shape3comboBox.SelectedIndex) {
  140. case 0:
  141. op.Union (r2);
  142. break;
  143. case 1:
  144. op.Intersect (r2);
  145. break;
  146. case 2:
  147. op.Exclude (r2);
  148. break;
  149. case 3:
  150. op.Complement (r2);
  151. break;
  152. case 4:
  153. op.Xor (r2);
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. private void shape3_SelectedIndexChanged (object sender, EventArgs e)
  160. {
  161. UpdateShape3 ();
  162. UpdateUI ();
  163. }
  164. private Matrix GetMatrix (ComboBox cb)
  165. {
  166. return Samples.Common.Matrices.GetMatrix (cb.SelectedIndex);
  167. }
  168. private void matrix1_Changed (object sender, EventArgs e)
  169. {
  170. if (r1 != null) {
  171. // an earlier matrix could be applied
  172. UpdateShape1 ();
  173. Matrix m = GetMatrix (matrix1comboBox);
  174. if (m != null) {
  175. r1.Transform (m);
  176. m.Dispose ();
  177. }
  178. // this can also affects the resulting operation
  179. matrix3_Changed (sender, e);
  180. UpdateUI ();
  181. }
  182. }
  183. private void matrix2_Changed (object sender, EventArgs e)
  184. {
  185. if (r2 != null) {
  186. // an earlier matrix could be applied
  187. UpdateShape2 ();
  188. Matrix m = GetMatrix (matrix2comboBox);
  189. if (m != null) {
  190. r2.Transform (m);
  191. m.Dispose ();
  192. }
  193. // this can also affects the resulting operation
  194. matrix3_Changed (sender, e);
  195. UpdateUI ();
  196. }
  197. }
  198. private void matrix3_Changed (object sender, EventArgs e)
  199. {
  200. if (op != null) {
  201. // an earlier matrix could be applied
  202. UpdateShape3 ();
  203. Matrix m = GetMatrix (matrix3comboBox);
  204. if (m != null) {
  205. op.Transform (m);
  206. m.Dispose ();
  207. }
  208. UpdateUI ();
  209. }
  210. }
  211. private void OnDisplayChanged (object sender, EventArgs e)
  212. {
  213. UpdateUI ();
  214. }
  215. protected override void OnMouseDown (MouseEventArgs e)
  216. {
  217. string msg = System.String.Empty;
  218. if (r1 != null) {
  219. if (r1.IsVisible (e.X, e.Y, gfx))
  220. msg = "shape1 ";
  221. }
  222. if (r2 != null) {
  223. if (r2.IsVisible (e.X, e.Y, gfx))
  224. msg += "shape2 ";
  225. }
  226. if (op != null) {
  227. if (op.IsVisible (e.X, e.Y, gfx))
  228. msg += "operation";
  229. }
  230. if (msg.Length > 0) {
  231. infoLabel.Text = System.String.Format ("Click ({0},{1}) is inside: {2}",
  232. e.X, e.Y, msg);
  233. } else {
  234. infoLabel.Text = System.String.Format ("Click ({0},{1}) is outside any region",
  235. e.X, e.Y);
  236. }
  237. base.OnMouseDown (e);
  238. }
  239. [STAThread]
  240. static void Main ()
  241. {
  242. Application.EnableVisualStyles ();
  243. Application.SetCompatibleTextRenderingDefault (false);
  244. Application.Run (new binary ());
  245. }
  246. }
  247. }