| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- //
- // binary.cs
- //
- // Authors:
- // Sebastien Pouliot <[email protected]>
- //
- // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Text;
- using System.Windows.Forms;
- namespace Samples {
- public partial class binary: Form {
- private Region r1;
- private Region r2;
- private Region op;
- private Graphics gfx;
- public binary ()
- {
- InitializeComponent ();
- object[] shapes = Samples.Common.Shapes.GetList ();
- shape1ComboBox.Items.AddRange (shapes);
- shape2ComboBox.Items.AddRange (shapes);
- object[] ops = new object[] {
- "Union",
- "Intersect",
- "Exclude",
- "Complement",
- "Xor"
- };
- shape3comboBox.Items.AddRange (ops);
- object[] matrices = Samples.Common.Matrices.GetList ();
- matrix1comboBox.Items.AddRange (matrices);
- matrix2comboBox.Items.AddRange (matrices);
- matrix3comboBox.Items.AddRange (matrices);
- }
- private void Form1_Paint (object sender, PaintEventArgs e)
- {
- gfx = e.Graphics;
- if (shape1checkBox.Checked && (r1 != null))
- gfx.FillRegion (Brushes.Red, r1);
- if (shape2checkBox.Checked && (r2 != null))
- gfx.FillRegion (Brushes.Green, r2);
- if (shape3checkBox.Checked && (op != null))
- gfx.FillRegion (Brushes.Blue, op);
- }
- private void resetButton_Click (object sender, EventArgs e)
- {
- shape1ComboBox.SelectedIndex = -1;
- if (r1 != null) {
- r1.Dispose ();
- r1 = null;
- }
- shape2ComboBox.SelectedIndex = -1;
- if (r2 != null) {
- r2.Dispose ();
- r2 = null;
- }
- if (op != null) {
- op.Dispose ();
- op = null;
- }
- UpdateUI ();
- }
- private void UpdateUI ()
- {
- Invalidate ();
- Update ();
- }
- private GraphicsPath GetShape (ComboBox cb)
- {
- return Samples.Common.Shapes.GetShape (cb.SelectedIndex);
- }
- private void UpdateShape1 ()
- {
- GraphicsPath path = GetShape (shape1ComboBox);
- if (path != null) {
- r1 = new Region (path);
- path.Dispose ();
- }
- }
- private void shape1_Changed (object sender, EventArgs e)
- {
- UpdateShape1 ();
- if (op != null) {
- op.Dispose ();
- op = null;
- }
- UpdateUI ();
- }
- private void UpdateShape2 ()
- {
- GraphicsPath path = GetShape (shape2ComboBox);
- if (path != null) {
- r2 = new Region (path);
- path.Dispose ();
- }
- }
- private void shape2_Changed (object sender, EventArgs e)
- {
- UpdateShape2 ();
- if (op != null) {
- op.Dispose ();
- op = null;
- }
- UpdateUI ();
- }
- private void UpdateShape3 ()
- {
- if (shape3comboBox.SelectedIndex == -1)
- return;
- if (r1 != null) {
- if (op != null)
- op.Dispose ();
- op = r1.Clone ();
- if (r2 != null) {
- switch (shape3comboBox.SelectedIndex) {
- case 0:
- op.Union (r2);
- break;
- case 1:
- op.Intersect (r2);
- break;
- case 2:
- op.Exclude (r2);
- break;
- case 3:
- op.Complement (r2);
- break;
- case 4:
- op.Xor (r2);
- break;
- }
- }
- }
- }
- private void shape3_SelectedIndexChanged (object sender, EventArgs e)
- {
- UpdateShape3 ();
- UpdateUI ();
- }
- private Matrix GetMatrix (ComboBox cb)
- {
- return Samples.Common.Matrices.GetMatrix (cb.SelectedIndex);
- }
- private void matrix1_Changed (object sender, EventArgs e)
- {
- if (r1 != null) {
- // an earlier matrix could be applied
- UpdateShape1 ();
- Matrix m = GetMatrix (matrix1comboBox);
- if (m != null) {
- r1.Transform (m);
- m.Dispose ();
- }
- // this can also affects the resulting operation
- matrix3_Changed (sender, e);
- UpdateUI ();
- }
- }
- private void matrix2_Changed (object sender, EventArgs e)
- {
- if (r2 != null) {
- // an earlier matrix could be applied
- UpdateShape2 ();
- Matrix m = GetMatrix (matrix2comboBox);
- if (m != null) {
- r2.Transform (m);
- m.Dispose ();
- }
- // this can also affects the resulting operation
- matrix3_Changed (sender, e);
- UpdateUI ();
- }
- }
- private void matrix3_Changed (object sender, EventArgs e)
- {
- if (op != null) {
- // an earlier matrix could be applied
- UpdateShape3 ();
- Matrix m = GetMatrix (matrix3comboBox);
- if (m != null) {
- op.Transform (m);
- m.Dispose ();
- }
- UpdateUI ();
- }
- }
- private void OnDisplayChanged (object sender, EventArgs e)
- {
- UpdateUI ();
- }
- protected override void OnMouseDown (MouseEventArgs e)
- {
- string msg = System.String.Empty;
- if (r1 != null) {
- if (r1.IsVisible (e.X, e.Y, gfx))
- msg = "shape1 ";
- }
- if (r2 != null) {
- if (r2.IsVisible (e.X, e.Y, gfx))
- msg += "shape2 ";
- }
- if (op != null) {
- if (op.IsVisible (e.X, e.Y, gfx))
- msg += "operation";
- }
- if (msg.Length > 0) {
- infoLabel.Text = System.String.Format ("Click ({0},{1}) is inside: {2}",
- e.X, e.Y, msg);
- } else {
- infoLabel.Text = System.String.Format ("Click ({0},{1}) is outside any region",
- e.X, e.Y);
- }
- base.OnMouseDown (e);
- }
- [STAThread]
- static void Main ()
- {
- Application.EnableVisualStyles ();
- Application.SetCompatibleTextRenderingDefault (false);
- Application.Run (new binary ());
- }
- }
- }
|