-
- Posts: 14
- Joined: 19 Feb 2014 10:37
Create User Control
Hello,
I'm trying to make a minesweeper and i would like to ask you if I can create a new user control (Button in my case) with specific properties.
So on load form i'll be able to use a recursive function to print 81 buttons instead of add 81 buttons.
Is there a way to make this ??
Kind regards
I'm trying to make a minesweeper and i would like to ask you if I can create a new user control (Button in my case) with specific properties.
So on load form i'll be able to use a recursive function to print 81 buttons instead of add 81 buttons.
Is there a way to make this ??
Kind regards
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
-
- Posts: 14
- Joined: 19 Feb 2014 10:37
Hello, thank you for the response.
Actually i'm working on it in other way, what i want to make is a form with 81 buttons in a form (9 rows and 9 columns).
What i want to do is:
1) To give to each button coordinates, for example 1.1, 1.2, 1.3 ... 2.1, 2.2, 2.3
so when i want to get the value of button in position 1.1 to have a function like value(X,Y)
The question here is how can i add that kind of label(cooordinates(X,Y)) for a button ???
What i want to achieve with this is that when i press a button with coordinates 3.3 to check all it's neighbours.
For example, neigbours of 3.3 = 2.2, 2.3, 2.4, 3.2, 3.4, 4.2, 4.3, 4.4.
2) The second question is if there is any random function in Visual Prolog ?? Because i want everytime to have random position for the mines (which will be in the buttons propably as a label)
Thank you in advance for your help
Actually i'm working on it in other way, what i want to make is a form with 81 buttons in a form (9 rows and 9 columns).
What i want to do is:
1) To give to each button coordinates, for example 1.1, 1.2, 1.3 ... 2.1, 2.2, 2.3
so when i want to get the value of button in position 1.1 to have a function like value(X,Y)
The question here is how can i add that kind of label(cooordinates(X,Y)) for a button ???
What i want to achieve with this is that when i press a button with coordinates 3.3 to check all it's neighbours.
For example, neigbours of 3.3 = 2.2, 2.3, 2.4, 3.2, 3.4, 4.2, 4.3, 4.4.
2) The second question is if there is any random function in Visual Prolog ?? Because i want everytime to have random position for the mines (which will be in the buttons propably as a label)
Thank you in advance for your help
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
OK, now it is getting clearer.
I think you could make a faily good minesweeper look and feel if you draw the board with all the numbers and mines, and then place buttons on the top.
When the user presses a button you will simply destroy the button and do the relevant action (which may include updating the board drawing to indicate a lost game).
Alternatively, you can replace pressed buttons with an image (of fields with nothing/a number/a mine/...), using image control.
Anyways, to place the buttons you can use code like this (in a form):
I think you could make a faily good minesweeper look and feel if you draw the board with all the numbers and mines, and then place buttons on the top.
When the user presses a button you will simply destroy the button and do the relevant action (which may include updating the board drawing to indicate a lost game).
Alternatively, you can replace pressed buttons with an image (of fields with nothing/a number/a mine/...), using image control.
Anyways, to place the buttons you can use code like this (in a form):
Code: Select all
constants
columns = 9.
rows = 9.
size = 10.
clauses
new(Parent):-
formWindow::new(Parent),
generatedInitialize(),
foreach Row = std::cIterate(rows), Column = std::cIterate(columns) do
Button = button::new(This),
placeInGrid(Button, Row, Column),
Button:setClickResponder({ = button::noAction :- onClick(Button, Row, Column) })
end foreach.
predicates
onClick : (button Button, positive Row, positive Column).
clauses
onClick(Button, Row, Column) :-
Button:destroy(),
Text = textControl::new(This),
placeInGrid(Text, Row, Column),
Text:setAlignment(vpiDomains::alignCenter),
Text:setText("x"),
Text:show(),
stdio::writef("(%, %)\n", Row, Column).
predicates
placeInGrid : (control Control, positive Row, positive Column).
clauses
placeInGrid(Control, Row, Column) :-
Control:setPosition(Column*size, Row*size),
Control:setSize(size, size).
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
Here is a slightly updated version, that stores the buttons in a map, so that they can also be removed without being clicked (for example to reveal the solution in the end):
Code: Select all
facts
buttons : mapM{tuple{positive Row, positive Column}, button Button} := mapM_redBlack::new().
constants
columns = 9.
rows = 9.
size = 10.
clauses
new(Parent):-
formWindow::new(Parent),
generatedInitialize(),
foreach Row = std::cIterate(rows), Column = std::cIterate(columns) do
Button = button::new(This),
placeInGrid(Button, Row, Column),
Button:setClickResponder({ = button::noAction :- onClick(Row, Column) }),
buttons:set(tuple(Row, Column), Button)
end foreach.
predicates
onClick : (positive Row, positive Column).
clauses
onClick(Row, Column) :-
Field = tuple(Row, Column),
Button = buttons:get(Field),
buttons:removeKey(Field),
Button:destroy(),
Text = textControl::new(This),
placeInGrid(Text, Row, Column),
Text:setAlignment(vpiDomains::alignCenter),
Text:setText("x"),
Text:show(),
stdio::writef("(%, %)\n", Row, Column).
predicates
placeInGrid : (control Control, positive Row, positive Column).
clauses
placeInGrid(Control, Row, Column) :-
Control:setPosition(Column*size, Row*size),
Control:setSize(size, size).
Regards Thomas Linder Puls
PDC
PDC
-
- Posts: 14
- Joined: 19 Feb 2014 10:37
-
- Posts: 14
- Joined: 19 Feb 2014 10:37
Hello again,
I have tried this with the followin way but seems that i miss something.
1) I have created a new project. I have added a new in package -> Form
2) then task menu -> unchecked F7 disabled
3) then id_file_new -> onFileNew add
4) clauses
onFileNew(_Source, _MenuTag):-
_= mines::display(This).
so i can displa the form
and then I inserted the proposed code in my project and i get the following error:
"undeclared predicate, propert or fact 'new/1'"
Till step 4 this is the right way to follow ??? I have to make new window form and then insert the code for the button creation?
I have tried this with the followin way but seems that i miss something.
1) I have created a new project. I have added a new in package -> Form
2) then task menu -> unchecked F7 disabled
3) then id_file_new -> onFileNew add
4) clauses
onFileNew(_Source, _MenuTag):-
_= mines::display(This).
so i can displa the form
and then I inserted the proposed code in my project and i get the following error:
"undeclared predicate, propert or fact 'new/1'"
Till step 4 this is the right way to follow ??? I have to make new window form and then insert the code for the button creation?
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
-
- Posts: 14
- Joined: 19 Feb 2014 10:37
-
- Posts: 14
- Joined: 19 Feb 2014 10:37
Dear Thomas i have made a very nice program with your help.
At clause new(Parent) i have placed the for each loop to create buttons at rows and columns.
I have added to my window form 2 textboxes (for rows and col) and 1 button.
1) i want when i press the button to clear the form from the existing buttons
2) and then call the new(Parent) where i have the for each loop that creates the buttons
I have copied the for each loop into the onclick clause but nothing happened when i pressed the button
At clause new(Parent) i have placed the for each loop to create buttons at rows and columns.
I have added to my window form 2 textboxes (for rows and col) and 1 button.
1) i want when i press the button to clear the form from the existing buttons
2) and then call the new(Parent) where i have the for each loop that creates the buttons
I have copied the for each loop into the onclick clause but nothing happened when i pressed the button
-
- Posts: 2
- Joined: 14 May 2014 10:11
Hello!
I want to create the same game on prolog and have faced a problem.
In your code there is:
but how can I make a responder for right mouse click?
Thank you.
I want to create the same game on prolog and have faced a problem.
In your code there is:
Code: Select all
Button:setClickResponder({ = button::noAction :- onClick(Row, Column) }),
Thank you.
-
- VIP Member
- Posts: 1467
- Joined: 28 Feb 2000 0:01
Standard buttons does not have a behavior on right-mouse clicks.
But you can add mouse a mouse listener:
Simple code looks like this:
But you can add mouse a mouse listener:
Code: Select all
Button:addMouseDownListener(onButtonMouseDown),
Code: Select all
predicates
onButtonMouseDown : window::mouseDownListener.
clauses
onButtonMouseDown(_Source, Point, ShiftControlAlt, Button) :-
stdio::writef("Point = %, ShiftControlAlt = %, Button = %\n", Point, ShiftControlAlt, Button).
Regards Thomas Linder Puls
PDC
PDC