Wind Speed Display

Wind Speed Display

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Wind Speed Display

Post by Jazcash »

Wind Speed Display

Download the latest version from the widget database

Image

As this is my first widget, please slam me with critique regarding the code.
Last edited by Jazcash on 23 Dec 2012, 22:30, edited 8 times in total.
dansan
Server Owner & Developer
Posts: 1203
Joined: 29 May 2010, 23:40

Re: Wind Speed Display

Post by dansan »

Very nice - I like it :)

I got

Code: Select all

[f=0000862] Loading:  LuaUI/Widgets/gui_windSpeed.lua
[f=0000862] [Font] Error: Failed creating font: Couldn't find font 'luaui/fonts/FreeSansBold.otf'.
[f=0000863] false
[f=0000863] Error in DrawScreen(): [string "LuaUI/Widgets/gui_windSpeed.lua"]:78: attempt to index upvalue 'Myfont' (a nil value)
[f=0000863] Removed widget: Wind Speed
Linux files and dirs are case sensitive, so I had to replace the path in line 41 with "LuaUI/Fonts/FreeSansBold.otf", and it worked.
I looked at other widgets, and they all use exactly this case sensitive path.

It has a bug in case of Tundra with min=max wind --> always red.

I don't get that nice windmill icon you have :( Maybe it is some transparency/opaque setting of mine? any idea?

Not your widgets fault: in BA max. 25 E of a windmill is used, even if wind is stronger. Is there a way to find out such max-settings of a game? Is it possible for a map to change that? IMO the green should be at min(max(map), max(game)).
Attachments
opaque icon
opaque icon
screen00029.png (1.11 KiB) Viewed 1633 times
User avatar
Niobium
Posts: 456
Joined: 07 Dec 2008, 02:35

Re: Wind Speed Display

Post by Niobium »

Theres no need for any font handling whatsoever:

Code: Select all

gl.Text(format('%.1f', wind), iconWidth + 13, 15, MyFontSize)
Only in tweak mode is easy, just add 'Tweak' to the callins:

Code: Select all

function widget:TweakMousePress(mx, my, mButton)
function widget:TweakMouseMove(mx, my, dx, dy, mButton)
Given someone has problems with the texture, I'd remove the texture options and go with:

Code: Select all

gl.Texture('LuaUI/Images/Windicon.png')
You'll also want to put add

Code: Select all

gl.Texture(false)
Right after you've used the texture, otherwise it stays set and can interfere with other widgets.
dansan
Server Owner & Developer
Posts: 1203
Joined: 29 May 2010, 23:40

Re: Wind Speed Display

Post by dansan »

Niobium wrote:Given someone has problems with the texture, I'd remove the texture options and go with:

Code: Select all

gl.Texture('LuaUI/Images/Windicon.png')
Works - I see a windmill :)
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

dansan wrote:It has a bug in case of Tundra with min=max wind --> always red.
I'll fix this, perhaps so it's just 100% green, or maybe set it grey or white or something to represent that it doesn't change?
dansan wrote:Not your widgets fault: in BA max. 25 E of a windmill is used, even if wind is stronger. Is there a way to find out such max-settings of a game? Is it possible for a map to change that? IMO the green should be at min(max(map), max(game)).
Not sure if there is a way to fetch a game's max windmill energy usage but I guess it wouldn't be too hard to setup an array for the top 5 games or something with their max windmill energy hard-coded. It'd just require changing if a game changes it.
Niobium wrote:Theres no need for any font handling whatsoever:

Code: Select all

gl.Text(format('%.1f', wind), iconWidth + 13, 15, MyFontSize)
I did do it this way originally, but changed it when I struggled finding good examples of using other fonts with it. Looks like I don't need to though, silly me
Niobium wrote:Only in tweak mode is easy, just add 'Tweak' to the callins:

Code: Select all

function widget:TweakMousePress(mx, my, mButton)
function widget:TweakMouseMove(mx, my, dx, dy, mButton)
Simple enough!
Niobium wrote:Given someone has problems with the texture, I'd remove the texture options and go with:

Code: Select all

gl.Texture('LuaUI/Images/Windicon.png')
Alright, I didn't account for how other OSs handle files and directories and such.
Niobium wrote:You'll also want to put add

Code: Select all

gl.Texture(false)
Right after you've used the texture, otherwise it stays set and can interfere with other widgets.
Alrighty

Thanks Niobium and dansan! I'll release the next version with these changes once I've figured out how rotate images around a point of origin in OpenGL, seeing as there's no point of origin functionality at all, I can see it being difficult :(
User avatar
very_bad_soldier
Posts: 1387
Joined: 20 Feb 2007, 01:10

Re: Wind Speed Display

Post by very_bad_soldier »

I think the rotation happens around the axis origin. So you can move the object so that the origin is at your desired rotation point, then rotate and then move to the final position.

EDIT:
Dunno if its really desired to have it rotating. Might be annoying to have a permanently rotating GUI element, no? Who gave you that shitty idea? :regret:
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

very_bad_soldier wrote: EDIT:
Dunno if its really desired to have it rotating. Might be annoying to have a permanently rotating GUI element, no? Who gave you that shitty idea? :regret:
I already thought of that, and was gonna make it a configurable option :-P

Then again, I think it might be more trouble that it's worth...
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

Updated!

Changelog:

Code: Select all

- No longer includes a font file
- Only moveable in tweak-mode
- Colour is green instead of red on maps where min and max wind are equal
- Blades on the windmill rotate according to the wind percentage
- Is hidden until the game begins
The blade rotation may be ugly. For me, I can see some artifacting (right word?). Not sure if this can be prevented.

Anyway you can turn off rotation by just changing the configRotation variable to 0, it's right at the bottom of my global variables. I'll perhaps had a nice GUI way to turn it on and off at some point.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Wind Speed Display

Post by knorke »

you read Spring.GetWind and in DrawScreen() which is very often but wind only changes slowly.
So read it in GameFrame() instead and calculate color there too.

If you increase rotation variable in DrawScreen the windmill will spin faster the more FPS you have.
Use Update (dt) instead.

Neither will not make a difference in speed for this widget but for more complex things it might and since you asked for critique ;)
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

knorke wrote:you read Spring.GetWind and in DrawScreen() which is very often but wind only changes slowly.
So read it in GameFrame() instead and calculate color there too.

If you increase rotation variable in DrawScreen the windmill will spin faster the more FPS you have.
Use Update (dt) instead.

Neither will not make a difference in speed for this widget but for more complex things it might and since you asked for critique ;)
Just been speaking with vbs about this too, I'll see what I can do, thanks! :-)
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

Any ideas how to fix this artifacting?

Image

The image is a png and artifacting only seems to occur when the rotation isn't 0, 90, 180 or 270.


Edit: Now fixed, the blade image just added more white-space around it due to exceeding its size limit when rotating. Thanks to BD for pointing this out, silly I couldn't see it myself :D
Last edited by Jazcash on 08 Dec 2012, 17:49, edited 3 times in total.
User avatar
Jools
XTA Developer
Posts: 2813
Joined: 23 Feb 2009, 16:29

Re: Wind Speed Display

Post by Jools »

I think it has become spring standard to allow move of widgets when you press the middle mouse button and drag it. I think that could be a nice option.

Tweak mode screws up ctrl key state.
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

Jools wrote:I think it has become spring standard to allow move of widgets when you press the middle mouse button and drag it. I think that could be a nice option.

Tweak mode screws up ctrl key state.
A few people told me they didn't like the fact it could be moved out of tweak mode as they sometimes usually move widgets that have this functionality by accident. I've never had this problem myself, might be due to my huge monitor but it would be nice to know what the majority think should be the default way to move a widget's position.

Although I don't have a problem with accidentally moving widgets, I still think all moving should be done in tweak mode anyway.
User avatar
very_bad_soldier
Posts: 1387
Joined: 20 Feb 2007, 01:10

Re: Wind Speed Display

Post by very_bad_soldier »

Jazcash wrote:Any ideas how to fix this artifacting?
Your texture dimensions need to be the power of 2. So resize the image (do not scale, resize the "canvas") and then grab the actually needed region by using texture coordinates.
Thats at least how I did it.
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

very_bad_soldier wrote:
Jazcash wrote:Any ideas how to fix this artifacting?
Your texture dimensions need to be the power of 2. So resize the image (do not scale, resize the "canvas") and then grab the actually needed region by using texture coordinates.
Thats at least how I did it.
Already fixed before reading this, but thanks anywho! :P
Now fixed, the blade image just added more white-space around it due to exceeding its size limit when rotating. Thanks to BD for pointing this out, silly I couldn't see it myself :D
dansan
Server Owner & Developer
Posts: 1203
Joined: 29 May 2010, 23:40

Re: Wind Speed Display

Post by dansan »

Wow - quick development! Looks very nice :)
User avatar
very_bad_soldier
Posts: 1387
Joined: 20 Feb 2007, 01:10

Re: Wind Speed Display

Post by very_bad_soldier »

Jazcash wrote:Now fixed, the blade image just added more white-space around it due to exceeding its size limit when rotating.
Hm I dont get that, it exceeds size limit when rotation?
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

very_bad_soldier wrote:
Jazcash wrote:Now fixed, the blade image just added more white-space around it due to exceeding its size limit when rotating.
Hm I dont get that, it exceeds size limit when rotation?
I don't fully understand it either, which is naughty of me. This is what I think was the issue, or at least, may be related in some way...

Image

It's amazing how little I care for why bugs occured once they're fixed xD

Anyway, it's looking almost bug free now. Next version will be released once I've added GUI options (with check-boxes). I'm guessing I have to code them myself or is there some sort of GUI element package out there I could use including things like slides, buttons, check-boxes and stuff?
User avatar
Floris
Balanced Annihilation Developer
Posts: 608
Joined: 04 Jan 2011, 20:00

Re: Wind Speed Display

Post by Floris »

This surely looks nice and clean comparing with other wind power display widgets!


Maybe change the color change red/green to relative effectiveness comparing with solar power?

It makes it more aperant on which windspeeds/ building winds is more effective to the average or newbie players.
User avatar
Jazcash
Posts: 5302
Joined: 08 Dec 2007, 17:39

Re: Wind Speed Display

Post by Jazcash »

Floris wrote: Maybe change the color change red/green to relative effectiveness comparing with solar power?

It makes it more aperant on which windspeeds/ building winds is more effective to the average or newbie players.
Yep, already on my to-do list as a configurable option!
Post Reply

Return to “Lua Scripts”