Page 1 of 1

Scripting a menu in celx

Posted: 20.01.2011, 19:58
by jan stegehuis
I had this prepared, to upload to the motherlode, but I don't think that will happen any time soon or at all.
It 's a simplified version of the menu structure I used in "goto for spacecraft" that can easily be adapted to
other menu needs.

Menu.celx

Code: Select all

-- Title: Menu example

--[[ Description

This is a simple menu that can serve as an example for how to create
a menu using Celx scripting. The example can be easily adjusted to
your own needs.

The script displays a menu when the key M (Shift + M) is pressed and
prints a message when one of the key's J, K or L (Uppercase !!!) is
pressed in response to the menu.

The script uses lua's facility to create an (object oriented) type of
object; in this case the menu object. As long as the script runs,
the menu object is in one of two states:

   1.   menu.display: waiting for the key "M" to be pressed
      - this is also the default state
   2.   menu.choice: waiting for a menu-choice to be pressed
      - this state ends when any key is pressed

]]

--[[ Menu Structure
=======================================================================
   a structure/object that holds all the data and logic (functions
   and methods) to form a base for a 'menu' functionality
=======================================================================]]

--[[ Table: menu
------------------------------------------------------------------------
   holds menu data, functions, and state
------------------------------------------------------------------------]]
menu   = {
   state         = nil,
   display         = {
      keys      = "M",
      functions   = {
         M      = displayMenu
      }
   },
   choice         = {
      keys      = "JKL",
      functions   = {
         J      = printJ,
         K      = printK,
         L      = printL
      }
   }
}
-- set state to default!!!
-- -----------------------
menu.state = menu.display
-- -----------------------


--[[ State method: menu.display:wantsKey()
------------------------------------------------------------------------
   current state == display; if key has correct value, accept key as input
------------------------------------------------------------------------]]
function menu.display:wantsKey(eventInfo)
   if string.find(self.keys, eventInfo.char) == nil then
      menu:stateReset()
      return false
   else
      return true
   end
end


--[[ State method: menu.display:processKey()
------------------------------------------------------------------------
   current state == display; process the key entered by the user
------------------------------------------------------------------------]]
function menu.display:processKey(eventInfo)
   return self.functions[eventInfo.char](self)
   -- ".functions[](self)" is needed here; Lua will n?t resolve: ":functions[]()" correctly
end


--[[ State method: menu.choice:wantsKey()
------------------------------------------------------------------------
   current state == choice; if key has correct value, accept key as input
------------------------------------------------------------------------]]
function menu.choice:wantsKey(eventInfo)
   if string.find(self.keys, eventInfo.char) == nil then
      menu:stateReset()
      return false
   else
      return true
   end
end


--[[ State method: menu.choice:processKey()
------------------------------------------------------------------------
   current state == choice; process the key entered by the user
------------------------------------------------------------------------]]
function menu.choice:processKey(eventInfo)
   return self.functions[eventInfo.char](self)
   -- ".functions[](self)" is needed here; Lua will n?t resolve: ":functions[]()" correctly
end


--[[ Method: menu:stateReset()
------------------------------------------------------------------------
   resets state to default
------------------------------------------------------------------------]]
function menu:stateReset()
   self.state = self.display
end

                                       
--[[ Method: menu:wantsKey()
------------------------------------------------------------------------
   check if menu wants to process the key the user pressed
------------------------------------------------------------------------]]
function menu:wantsKey(eventInfo)
   celestia:print("")      -- remove possible previously printed text
   if self.state == nil then
      return false
   else
      return self.state:wantsKey(eventInfo)
   end
end


--[[ Method: menu:processKey()
------------------------------------------------------------------------
   allow menu to process the key the user pressed
------------------------------------------------------------------------]]
function menu:processKey(eventInfo)
   if self.state == nil then
      return false
   else
      return self.state:processKey(eventInfo)
   end
end


--[[ Testers
========================================================================
Test functions to show how the menu structure can be used
=======================================================================]]
-- print the menu
function displayMenu()
   celestia:print("J = Choice 1\nK = Choice 2\nL = Choice 3\n")
   menu.state = menu.choice
   return true
end

-- confirm menu choice J
function printJ()
   celestia:print("Menu choice = J")
   menu:stateReset()
   return true
end

-- confirm menu choice K
function printK()
   celestia:print("Menu choice = K")
   menu:stateReset()
   return true
end

-- confirm menu choice L
function printL()
   celestia:print("Menu choice = L")
   menu:stateReset()
   return true
end


--[[ Script Main:
=======================================================================
   The main functions
=======================================================================]]

--[[ Function: keyEvent()
------------------------------------------------------------------------
   the keyboard event handler
------------------------------------------------------------------------]]
function keyEvent(eventInfo)
   if menu:wantsKey(eventInfo) then
      return menu:processKey(eventInfo)
   -- elseif otherMenu:wantsKey(eventInfo) then
      -- return otherMenu:processKey(eventInfo)
   else
      return false
   end
end


--[[ Function: Main()
------------------------------------------------------------------------
   the function Main
------------------------------------------------------------------------]]
function Main()
   celestia:registereventhandler("key", keyEvent)
   celestia:print("Menu is active", 2)
end


--[[ Script
------------------------------------------------------------------------
   start
------------------------------------------------------------------------]]
Main()


Have fun using it.

Jan

Re: Scripting a menu in celx

Posted: 21.01.2011, 10:04
by jan stegehuis
Next to [Key] and [Shift]+[Key] combinations, you can also use [Control]+[Key] combinations for your menu. None of the [Alt]+[Key] combinations however. [Ctrl]+[Shift]+[Key] combinations are not supported either but are reported as [Ctrl]+[Key] combinations, so pressing [Ctrl]+[Shift]+[A] has the same result as pressing [Ctrl]+[A].

In other object oriented type environments "key state" info like [Shift] would be part of the event data packet; somewhat like this:

Code: Select all

eventData{
   char     = string: the character pressed,
   shift    = boolean: the shift key was or was not pressed,
   control  = boolean: the control key was or was not pressed,
   alt      = boolean: the alt key was or was not pressed
   }

Not in Celestia.celx however; both character ?nd state information is combined in the "char" field. For the keyboard key [A] for example this would result in one of the following:

Code: Select all

   Key pressed      Value of keyevent.char
   -----------      ----------------------
   [A]              = "a"
   [Shift]+[A]      = "A"
   [Ctrl]+[A]       = "C-a"

Not all key's yield a keyevent; the ones that don't comprise keys like the function and cursor keys. Also not all keypresses result in what you would expect. Pressing the [Enter] key for instance results in "C-m" and pressing [Ctrl]+[Entert] in "C-j". So before you start using certain key combinations, you'd better write a little test script to see what happens. Like this one for example.

Code: Select all

--[[ Script Main:
=======================================================================
   The main functions
=======================================================================]]
intercept = false

--[[ Function: keyEvent()
------------------------------------------------------------------------
   the keyboard event handler
------------------------------------------------------------------------]]
function keyEvent(eventInfo)
   if intercept == false then
      if eventInfo.char == "i" or eventInfo.char == "I" then
         intercept = true
         celestia:print("Started key interception", 2)
         return true
      else
         return false
      end
   else
      if eventInfo.char == "i" or eventInfo.char == "I" then
         intercept = false
         celestia:print("Stopped key interception", 2)
         return true
      else
         celestia:print("keyEvent.char = " ..eventInfo.char)
         return true
      end
   end
end


--[[ Function: Main()
------------------------------------------------------------------------
   the function Main
------------------------------------------------------------------------]]
function Main()
   celestia:registereventhandler("key", keyEvent)
   celestia:print("Key test is active", 2)
end


--[[ Script
------------------------------------------------------------------------
   start
------------------------------------------------------------------------]]
Main()


Jan

Re: Scripting a menu in celx

Posted: 21.01.2011, 14:32
by John Van Vliet
--- edit ---

Re: Scripting a menu in celx

Posted: 21.01.2011, 18:09
by Adirondack
jan stegehuis wrote:I had this prepared, to upload to the motherlode, but I don't think that will happen any time soon or at all.
We are all volunteers and we do have another life too. Please be patient after upload. Thanks.

Adirondack

Re: Scripting a menu in celx

Posted: 21.01.2011, 22:00
by jan stegehuis
Sure, other people don't have a life of their own, they sit around all day waiting for something to appear on the lode. I'm not retired yet gentlemen, not for a long time.

Selden spoke of a couple of days and in my dictionary a couple is still described as "two". I did not read that post until after I uploaded the first version. What got me was the fact that any subsequent update would also take that long. Had I known up front that it would even take more than a week, I would most certainly never have uploaded it to begin with. Not just more than a week, but more than a week of utter silence; not even a short message that your other life takes too much of your time, but that things will be taken care of as soon as possible.

So do me a favour, forget that I ever uploaded the script and don't put it on the lode!

Jan

Re: Scripting a menu in celx

Posted: 22.01.2011, 09:38
by MiR
Jan,

thanks for your work. :)

You can also publish smaller scripts here at shatters.net as an attachment (.zip file/ but not more than 250kb's).

Adirondack wrote: We are all volunteers and we do have another life too. Please be patient after upload. Thanks.

Adirondack
Without the texture enhancements I found more than thousands of Celestia add-ons on my harddisk. And I can imagine how much more work it makes testing every texture map, script, readme's and so on.

And I must say I don't exactly know how many hundreds of downloaded add-ons I haven't tested and installed yet.

Because it takes so much time to organize* it. :roll:

But in some cases it might be helpful if someone could give a probable date when it should take a longer time to publish the add-ons at CML. Because sometimes it helps if we hear the "work" is in progress...

Michael

*Therefore I add a picture to all of my work. To see what kind of changes it makes.

Re: Scripting a menu in celx

Posted: 22.01.2011, 14:09
by Adirondack
jan stegehuis wrote:So do me a favour, forget that I ever uploaded the script and don't put it on the lode!
Okay.
Thanks for your well-meaning understanding.
The world needs more people like you.

Adirondack

Re: Scripting a menu in celx

Posted: 26.01.2011, 21:53
by jan stegehuis
Thanks Michael,
I will see what I will do about publishing any future stuff. I can empathize with testing, etc being quite a job sometimes, the whole reason why I wrote the script in the first place was out of frustration over how I had to test a number of spacecraft objects I had downloaded from the ML. And this
But in some cases it might be helpful if someone could give a probable date when it should take a longer time to publish the add-ons at CML. Because sometimes it helps if we hear the "work" is in progress...
would definitely be the more decent and less offending way to go about it. Anybody can understand that things sometimes can take more time than expected, but nobody can understand a feeling of being ignored and kept in the dark.

We live in an age where uploading and publishing anything on the net is more a question of seconds than one of minutes, let alone of days or weeks. Most of the time when a site offers server space, that space comes with tools that allow one to maintain ones published documents oneself online.
So when I see an add/invitation stating that:
The Celestia Motherlode provides free server space for your add-on files!
I don't initially think of a situation where an uploaded document first has to go through a ballot committee where it gets tested and otherwise assessed, before it gets stored on a server where one then has no control over it oneself whatsoever.
I could to some extent live with that for an application like Celestia, but only when both the procedure and the average turnaround time are clearly mentioned on the site. So that one knows up front what one is up for and can choose a different solution if needed.

In this case it concerns a script, a piece of software and software can have bugs that need fixing. As a matter of principle I want to be able to publish a bugfix within the smallest possible timeframe after a bug has been found/reported; simply a question of responsibility and "customer service". I can't square that with the current situation around the motherlode, where after uploading a bugfix the script's users would have to wait an indefinite amount of time until somebody finally finds the time and the willingness to publish it.

So Adirondack
Thanks for not publishing, but your response is either insultingly sarcastic, in which case I feel sorry for you, or I really don't know what you're talking about.

Jan

Re: Scripting a menu in celx

Posted: 27.01.2011, 00:03
by Hungry4info
jan stegehuis wrote:I can empathize with testing, etc being quite a job sometimes, the whole reason why I wrote the script in the first place was out of frustration over how I had to test a number of spacecraft objects I had downloaded from the ML.
jan stegehuis wrote:I don't initially think of a situation where an uploaded document first has to go through a ballot committee where it gets tested and otherwise assessed, before it gets stored on a server where one then has no control over it oneself whatsoever.
eh??


jan stegehuis wrote:As a matter of principle I want to be able to publish a bugfix within the smallest possible timeframe after a bug has been found/reported; simply a question of responsibility and "customer service". I can't square that with the current situation around the motherlode, where after uploading a bugfix the script's users would have to wait an indefinite amount of time until somebody finally finds the time and the willingness to publish it.
This whole process isn't automated. People have to devote their personal time to getting these things done. It's unfortunate you don't seem to understand this.

Being kept in the dark? Nonsense. You got a response to your post explaining what was going on four hours after you posted it. That's not unreasonable for this public forum.

I echo Adirondack's words.

Re: Scripting a menu in celx

Posted: 27.01.2011, 12:03
by MiR
Hungry4info,

Hungry4info wrote:Being kept in the dark? Nonsense. You got a response to your post explaining what was going on four hours after you posted it.
Hm, meanwhile I've sent four add-ons to ML - Apollo Cockpits, Mars-Storm and now newVenusClouds (2011.01.03) and 51Pegasi/b (2011.01.13) - but never received any response with an explanation what's going on...

I would quickly like to add; As a newbie I've made two errors in the first two add-ons (Copyright-info, if I remember well), wrote Ulrich (Adirondack) a PM and every time he answered very quick and very friendly (and solved the problems - additional work, resulting from my mistake!)

So, I regret that unnecessary dispute, based on misunderstandings, very much. :oops: :cry:

jan stegehuis wrote:I don't initially think of a situation where an uploaded document first has to go through a ballot committee where it gets tested and
otherwise assessed, before it gets stored on a server where one then has no control over it oneself whatsoever.
Jan,

a careful review of the uploaded files is important, good for all of us. :idea:

We all want good, error-free add-ons: maps in simple cylindrical, spherical map projections, understandable readme's, checked and proofed copyrights and well-working scripts.

I really do not envy the guys because of their jobs.

<outoftopic> Poor ET. If you ever find the way down to our Earth, be well-prepared; You see, even in this little community we have difficulties to get along in friendship. Watch out and don't show any weakness... or we'll find a thousand good reasons to attack you... </outoftopic>

Michael

Re: Scripting a menu in celx

Posted: 27.01.2011, 16:47
by jogad
Eh jan!

People have been waiting for years to enjoy such a jewel of script. One week less or more really doesn't matter.
Are they crying and insulting people who offered their time and ability to serve the community? :(

Of course the ML is not perfect but exists! Thanks for that! :D
It is a hand made service like the old post office. Letters travel slowly but at last you get them. :wink:

Seriously your "goto for spacecraft" is a useful piece of software. It has a precise goal and do its job perfectly.
Most people could enjoy it :blue: :mrgreen: and the Motherlode is a possible suitable place for it.

Now let's consider your "celx menu". Elegant scripting but the script "as is" has not a real utility. Only advanced script makers could be interested. And for this kind of people, they can as well take example directly from your original "goto for spacecraft" script.

There are also other scripts that exhibit very smart menus and are part of a complete application.
Take a look at this:
http://www.celestiamotherlode.net/catal ... don_id=836

And I don't even speak about the amazing "lua edu tools" with its entirely graphic interface.

people in their ivory towers? :lol:

For small scripts like utilities, examples or tutorials, I consider this forum as an excellent place. You can update immediately and have a feedback from the other users. 8)

Re: Scripting a menu in celx

Posted: 27.01.2011, 23:24
by jan stegehuis
This whole thing has nothing to do with how the ML works and what I might think of that. It has nothing to do with the fact that it is "a hand made service like the old post office" or with the fact that "People have to devote their personal time to getting these things done". Or whether that is or is not unreasonable for the Celestia public.

I did not criticise how the ML procedures work, I criticised it's lack of information and communication. The not clearly informing new users up front - i.e. on the upload page or form - that they are dealing with an "old post office" so they know what they can expect. Not informing people about the average duration of the upload procedure and not letting them know timely when the duration of their upload will deviate from the average considerably.

Hungry4info wrote:
Being kept in the dark? Nonsense. You got a response to your post explaining what was going on four hours after you posted it. That's not unreasonable for this public forum.
Yes that would have been far from unreasonable ?f I realy would have been informed 4 hours after posting or even 8 or 12 or 24. The reality however is that - like MiR - I have not been informed of anything within any reasonable amount of time after posting at all. The first response I got from somebody recognizable as a representative of the ML was adirondacs post in the thread: "Goto for spacecrafts" on friday 21 january, 8 days after upload and not until ?fter I decided not to wait any longer and make my irritation known in this forum. For completeness: this is not about the celx menu in this thread but about the "spacecraft goto" script; see also the thread: "Possible bug(s) in Celx/Lua" where this all started.

jogad wrote:
Are they crying and insulting people who offered their time and ability to serve the community?
I did not insult anybody of that sort of behavior jogad or even hinted in that direction, I simply stated a personal opinion that I tend to feel quite strongly about.

Re: Scripting a menu in celx

Posted: 28.01.2011, 00:45
by Fenerit
Jan, in the forementioned thread "Possible bug(s) in Celx/Lua" you were asking for a place in which to post your works. I was suggesting to you in finding a personal (several are free) host service mainly because you can always modify its content, thus, if your scripts need to be updated and/or extended, everyone can access them with easiness from here. You probably had seen in Celestia Motherlode the most fitted room for your works, for then to find that the things weren't so peaceful; now, once clarified that, you must consider how this subforum is as many good as a "reference" link for all people interested in scripting (IMHO, is what Jogad was saying) Successively, when through your judgement the scripts are deliberated as finished and/or feedbacks from the users are positive, you can always send them on Motherlode; the time required for their hosting isn't important. Usually such method is followed by several add-ons makers: they promotes their works here, in the forum, and then send them to ML.

Re: Scripting a menu in celx

Posted: 28.01.2011, 11:42
by jan stegehuis
Fenerit,
All I wanted to do back then is to leave behind a copy of the script as a form of 'thank you' for those who had spent time helping me with some problems and as an example of what the final script had been all about. Failing to add it as an attachment to a post, I ended up inserting it into the post as a code block. It was you who then clarified the problem with the attachment and informed me of the correct procedure, involving: "finding a file hosting service in which to place the zipped file to link at as URL from here".

That looked like a lot of work for what I had meant to do and so it did not realy seem to be the way to go. I was happy to leave it as a code block on this forum. But then Selden posted: "You might consider having the script hosted by the CelestiaMotherlode.". And jogad followed with a post that showed an apparent interest in the script from the community, so I decided to follow selden's suggestion. Even though I didn't know what to expect from a file hosting service on the ML, putting it there seemed like a lot less of a hassle for just the one script than finding, configuring and maintaining something somewhere on some public file hosting service.

So I uploaded the script to the ML on thu Jan 13 and the waiting started. In the mean time I started a second version of the script that was better suited for publication than the first version, that was little more than an early beta. When I finished that on sun Jan 16, I had not yet heard anything about the first upload but decided to upload the second version anyway. I figured this would allow the ML team to discard the first version and only publish the second one.
Then the waiting continued. When thu Jan 20 arrived and I still had not heard as much as a syllable from the ML about any of the two uploads I realy had had enough.

I published version two as a code block in a new thread on this forum and the rest is the history as you can read it in this thread. A thread that isn't about the direct cause of all the trouble (the goto scipt), but that was started at about the same time and for the same reason. I continued the "discussion" in this thread because it seemd useless to continue it in both and because this one contained an insulting remark from adirondac.

I had no plans and have no plans to become an activer publisher/creator of celestia works for the sake of it and/or for having my name appear on the ML. I will follow jogad's suggestion to use this forum for any future stuff that I happen to come across. If I ever d? become serious about this your suggestion seems to be the way to go.

Jogad,
Thanks for your praising words about the script but let's leave things as they are at the moment. When the script is mature and turns out not to cause any trouble with other celestia stuff, it could still be uploaded to the ML. Depending of course on the willingness of people like adirondac, who thinks so highly of me.

Re: Scripting a menu in celx

Posted: 28.01.2011, 12:46
by Fenerit
Ok, Jan. Personally, I think that your scripts are "must have". You are welcome.

Re: Scripting a menu in celx

Posted: 30.01.2011, 12:23
by Marco Klunder
Jan,

Perfect idea :idea: an perfect result of your script. :blue:
I already incorparated it in the startup script, to have it available by default :!:
I really appreciate your work.

I regret the "misunderstanding" between you and the ML people.
Let me state here that I also appriciate the ML efforts of Ulrich, Bob and John very much. :blue:

Please lets burry the battle-axe on this point, smoke the pipe of peace, respect eachother and put our energy on future development of new ideas and Celestia implementations. And who knows, part of such (joined) development may result in an improved ML upload procedure :wink:

Marco

Re: Scripting a menu in celx

Posted: 30.01.2011, 14:22
by jan stegehuis
Thanks Marco, Fenerit, Jogad and others.

Glad to see people enjoy the script.
The misunderstanding realy wasn't any fun, but as far as I'm concerned, there isn't a hatchet to bury.

It turns out by the way that Google Sites is a good and easy alternative for file hosting. So over the weekend I created a Google Sites site and put the script there. I'll put any stuff that I do in the area of Celestia and scripting on the site and from there stuff could migrate to the ML as needed.

You can find the site on: https://sites.google.com/site/janscelestia.

Jan