View previous topic :: View next topic |
Author |
Message |
gekkonier
Joined: 27 Mar 2007 Posts: 28
|
Posted: Thu Mar 29, 2007 10:17 am Post subject: 3 little questions about event handling |
|
|
Hi community,
I have the following program for testing purposes (The questions are below this program):
Code: | private import dfl.all;
class form: Form
{
Label label1, label2;
this()
{
backColor = Color(0, 0, 0);
with(label1 = new Label())
{
backColor = Color(255, 0, 0);
bounds = Rect(0, 0, 100, 100);
parent = this;
click ~= &changecolor;
}
with(label2 = new Label())
{
backColor = Color(255, 0, 0);
bounds = Rect(100, 0, 100, 100);
parent = this;
click ~= &changecolor;
}
}
void changecolor(Object sender, EventArgs ea)
{
label1.backColor = Color(0,255,0);
}
}
int main()
{
Application.run(new form);
return 0;
} |
Question 1:
In the label creation its possible to add a click event, wich calls changecolor.
Is it possible to say in clickcolor which item was clicked? I tried to change the color of the item that was clicked, but my d, dfl and english skills are very low, so it didn't worked as intended for me:
Code: | void changecolor(Object sender, EventArgs ea)
{
// try to change the color of the clicked item...
sender.backColor = Color(0,255,0);
} |
Sure, its possible to write an function for every label, but what can I do for DRY?
Question 2:
Is it possible to find out in changecolor which mousekey was pressed?
Question 3:
Is it possible to add a function that is called, when i hit a key?
My Form has only labels on it, and lets say, i want to change the color of a label wenn I press the left key for example (and how does it work if I want to know in this function which key was pressed?)
If this all stands in the dfl-doc please point me to it, I will try to understand, but I didn't find anything useful for me pointing this issues.
Thank you very much for reading, gekkonier
ps.: Small bonusquestion:
Does there exist a "canvas" style widget, in which I can paint like in the dfl-example draw.d? I like to try to write my first games ever (minesweeper and tetris clones) and would like to learn first technices for it, where I think they may be useful to know. |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Thu Mar 29, 2007 11:02 am Post subject: Re: 3 little questions about event handling |
|
|
For question 1: you can change the "Object sender" to "Control sender" and what you're doing will work.
For question 2: "click" only means left mouse button. If you want regular mouse events, there's mouseDown and mouseUp (among others), which give you MouseEventArgs.button (of type MouseButtons) to determine which button.
For question 3: controls have keyDown, keyUp and keyPress events. These use KeyEventArgs and enum Keys.
For bonusquestion: you can just use any control (even simply Control) and use the paint events. There's a lot of stuff in Graphics, and MemoryGraphics can be used for an in-memory graphic. Doing this is pretty involving, and if you want this to be high-performant or fast-paced, it's pretty hard for these graphics to work well. |
|
Back to top |
|
|
gekkonier
Joined: 27 Mar 2007 Posts: 28
|
Posted: Fri Mar 30, 2007 12:16 am Post subject: |
|
|
Thank you very much for your explanation.
Have a nice day! |
|
Back to top |
|
|
gekkonier
Joined: 27 Mar 2007 Posts: 28
|
Posted: Fri Mar 30, 2007 2:41 am Post subject: |
|
|
Okay, Shortcuts will do the trick for my porposes:
My first small game will be a tetris alike game I think. If its ever done I will post it here, maybee you want to put it into your examples then?
But don't wait for it, maybe it will be done in two or more months or never , I don't have any knowledge about gamedesign, only webapps, databases and textprocessing.
Code: | private import dfl.all;
class form: Form
{
this()
{
clientSize = Size(200, 200);
backColor = Color(0, 0, 0);
addShortcut(Keys.LEFT, &keyleft);
addShortcut(Keys.RIGHT, &keyright);
}
private void keyleft(Object sender, FormShortcutEventArgs ea)
{
backColor = Color(0,255,0);
}
private void keyright(Object sender, FormShortcutEventArgs ea)
{
backColor = Color(255,0,0);
}
}
int main()
{
Application.run(new form);
return 0;
} |
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|