Clicking Text and Swaping the Text?
BlitzMax Forums/BlitzMax Beginners Area/Clicking Text and Swaping the Text?
| ||
Hello there, Let say You got 6 peoples David Graham Paul Dan Polo Leo IF I want to click on David and swap Leo then it will look like this Leo Leo Graham Paul Dan Polo David The questions I would like ask is How do I do that by clicking the text and swaping the text? |
| ||
Using the position of the top of the list and the height of each line, work out on which line, if any, the mouse button click occured. |
| ||
How do you do that in code please? Update: It about putting player position in the team hence why I ask how do I swap players with when clicking on text(player) and swap it....a code example of it would be nice to know how.... thank you Last edited 2011 |
| ||
I don't really understand what you are asking, sorry. Do you have a list of names, you want to click one name, click another name, and the positions are swapped? |
| ||
Do you have a list of names, you want to click one name, click another name, and the positions are swapped? That correct! :) |
| ||
This should get you started. The TextHeight and TextWidth functions are a little bit slow. If this is causing performance problems, you could store the height in a global variable (it should be the same for all names) and store the widths in a second array of integers, one for each name (remember to update this array if the names change).Strict Global names:String[] = ["David", "Graham", "Paul", "Dan", "Polo", "Leo"] Const names_offset_x = 50, names_offset_y = 50, names_padding = 5 Graphics 640, 480 While Not (KeyDown(KEY_ESCAPE) Or AppTerminate()) Cls For Local i = 0 Until names.Length Local name:String = names[i] ' find the starting point for that name Local name_x = names_offset_x, name_y = names_offset_y + i * (TextHeight(name) + names_padding) ' check if the mouse is over the name If over_text(name, name_x, name_y, MouseX(), MouseY()) SetColor 255, 0, 0 Else SetColor 255, 255, 255 End If ' draw that name DrawText name, name_x, name_y Next Flip Delay 1 Wend End ' returns if the points are overlapping with the text starting at that location Function over_text(text:String, textx, texty, pointx, pointy) Local width = TextWidth(text), height = TextHeight(text) If pointx >= (textx) And pointx < (textx + width) And pointy >= (texty) And pointy < (texty + height) Return True Else Return False End If End Function Last edited 2011 |
| ||
That is good one :) and now I going try do the players swap :) |
| ||
If you already know the positions of the names in the list:Function SwapNames_ByNr(Pos1%,Pos2%) Local locName$ locName=Names[Pos1] Names[Pos1]= Names[Pos2] Names[Pos2]=locName End Function If you only know the names of the guys: Function SwapNames_ByNames(Name1$ , Name2$) Local Pos1% , Pos2% , i% For Local i = 0 Until names.Length If Name1 = Names[i] Pos1=i ElseIf Name2 = Names[i] Pos2=i Endif Next ' now use the first function SwapNames_ByNr Pos1 , Pos 2 End Function |
| ||
thanks Midimaster :) |
| ||
An effective search function, but to be perfect could have a couple of modifications: When it's found the two names, it will keep searching until the end. If the list is very large (hundreds of names), you should put an early exit. What happens if the two names to swap are the same? What happens if a name is not found? You also declare i twice. Last edited 2011 |
| ||
for he is a beginner i did not want to confuse him with to much code! this basic sample shows the principle. And can be extended by trouble checks... |