Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Miniwindows ➜ Scrolling, wrapping miniwindow library

Scrolling, wrapping miniwindow library

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Manacle   (28 posts)  Bio
Date Sun 02 Jan 2011 12:53 AM (UTC)
Message
Almost all of the plugins I've written for myself in the last few weeks use entirely text based interfaces, so I wrote a library that handles most of the common requirements I've had from miniwindows.

It's long, slow and very sloppy because I'd never seen Lua before a few weeks ago and haven't seen any coding language since college. It does work reasonably well, however, and it's sped up the development of my other plugins.

If nothing else, if you're making a textish plugin and need a testing framework so you don't have to worry about the logistics of managing the miniwindow itself until everything else is working, this might do for you.

It's too long to post here, so I borrowed Google's bandwidth.

https://docs.google.com/leaf?id=0BzepDUveDH20MWI4YjBjYzctNjg4OS00MDlkLThmZDAtM2VkNzQ2NTJkN2Fk

(My campaign tracker in progress using the above library)
https://docs.google.com/leaf?id=0BzepDUveDH20ODJhY2I3OWItMzMzNy00NGJiLTk4NDAtMDY5Y2YxMzkyZDI5
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #1 on Sun 02 Jan 2011 03:22 AM (UTC)
Message
Thanks for sharing that ... and all the complimentary remarks at the start. :)

Your code looks nicely laid out and commented.


Just a couple of observations ...


  • Your _repeat function appears at first glance to replicate the functionality of string.rep.

    http://www.gammon.com.au/scripts/doc.php?lua=string.rep


  • I'm not sure about the functions named with a leading underscore. The leading underscore is generally intended for "compiler-writer" named functions, which doesn't really apply in your case (this is just a convention). I'm guessing you are using them for "local" helper functions. In that case it might be better to simply make them local functions so they aren't "exposed" to elsewhere.

    eg. instead of:

    
    function _calculateMinPixelHeight(w)
    	return	10 + 3*w.scrollbar.size.width +
    		w.line_height + w.titlebar.size.height
    end -- _calculateMinPixelHeight }}}
    


    have:

    
    local function calculateMinPixelHeight(w)
    	return	10 + 3*w.scrollbar.size.width +
    		w.line_height + w.titlebar.size.height
    end -- _calculateMinPixelHeight }}}
    


  • Personally I would return nil to indicate "no value" rather than -1. It is a bit more Lua-like, because nil means literally "no value here". However using -1 makes you wonder if the function might ever need to actually return -1 as a valid value.

    For example, instead of:

    
    function HotspotManager._getIndexOfHotspot(w, hsname) -- {{{
    	for i, arr in ipairs(HotspotManager_List) do
    		if arr.window.name == w.name and
    		  arr.hotspotname == hsname then
    			return i
    		end
    	end
    	
    	return -1
    end -- }}}
    


    You could do:

    
    function HotspotManager._getIndexOfHotspot(w, hsname) -- {{{
    	for i, arr in ipairs(HotspotManager_List) do
    		if arr.window.name == w.name and
    		  arr.hotspotname == hsname then
    			return i
    		end
    	end
    	
    	return nil
    end -- }}}
    


    Or even:

    
    function HotspotManager._getIndexOfHotspot(w, hsname) -- {{{
    	for i, arr in ipairs(HotspotManager_List) do
    		if arr.window.name == w.name and
    		  arr.hotspotname == hsname then
    			return i
    		end
    	end
    	
    end -- }}}
    


    (This works because in the absence of a return statement, any value assigned from the function will be set to nil).

    Then the asserts can change from:

    
    assert(index ~= -1, "Attempted to remove hotspot blah blah ...")
    


    to:

    
    assert(index, "Attempted to remove hotspot blah blah ...")
    


    And indeed, since assert returns the value passed to it, if non-nil, you can reduce this:

    
    local index = HotspotManager._getIndexOfHotspot(w, hsname)
    		
    	assert(index ~= -1, "Attempted to remove hotspot blah blah")
    


    to this:

    
    local index = assert (HotspotManager._getIndexOfHotspot(w, hsname), "Attempted to remove hotspot blah blah")
    


    This assigns and checks for non-nil in a single statement.


This is fairly minor nit-picking, but since you mention you are new to Lua I thought it might help to develop your Lua style (or is that, Lua-fu?).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Fiendish   USA  (2,555 posts)  Bio   Global Moderator
Date Reply #2 on Sun 02 Jan 2011 01:43 PM (UTC)

Amended on Sun 02 Jan 2011 01:59 PM (UTC) by Fiendish

Message
I'm reading through it. I like it so far, clearly you've put a lot of effort into this, though two things did stand out to me early on.

In LoadFont I think you need to call fonts = utils.getfontfamilies() again after doing
AddFont(GetInfo(66) .. "\\" .. w.font.name .. ".fon"), otherwise the "if fonts[]" checks won't pick up the newly added font.

And also you appear to have changed the math for dragging the scrollbar. Why?

You say that it's "slow". Do you have an idea of which parts are slow?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Manacle   (28 posts)  Bio
Date Reply #3 on Sun 02 Jan 2011 02:59 PM (UTC)
Message
Fiendish,

I appreciate the message about the fonts. I'll incorporate that change in my next pass through to clean up bugs (and so many there are, unfortunately). I didn't even make an attempt to understand how loading/using fonts actually works during my first pass.

The "slow" comment is really just preemptive defense. I think, for the most part, there aren't any gross inefficiencies in how the library does things. I just didn't design for speed or frequent (5 times/sec) updates; I certainly add many layers of redirection to any action the windows take. As far as I can tell, the wrapping is fast, but the window doesn't handle resizing well with huge amounts of text. That seemed to crop up when I added the inline text-based hotspots.

I actually didn't "change" the scroller math. The slider was the last hotspot I added, and I had a decent grasp on the callbacks at that point, so I wasn't pulling elements from your code any more. I don't actually use sliders often, but I needed the scroller to work in at least some sort of fashion to move on with development, so I shoved something in that worked conceptually and added some constants to get the scrolling action close enough to let me test everything together.

I know it doesn't work perfectly; I think there's even a TODO somewhere reminding me to get around to fixing it. I just haven't yet because it's close enough. Part of the reason for that is that before I fix the slider math, I really need to abstract out some more of the dirty math elements (text area dimensions, and in this case the scrollbar trough height). Before I do that, I want to figure out how the Lua OO syntax works and decide now whether that's worth the refactoring cost, because if it is, I'll do that before factoring out more of the graphical math.

If you're still looking at it, there's a new version up that fixes some fairly nasty bugs involving the destruction routines that managed to freeze out my client a few times.
Top

Posted by Fiendish   USA  (2,555 posts)  Bio   Global Moderator
Date Reply #4 on Fri 04 Mar 2011 09:04 AM (UTC)
Message
Just curious, are you still working on this?

https://github.com/fiendish/aardwolfclientpackage
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,656 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.