custom mouse cursor code

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

custom mouse cursor code

#1 Post by KimiYoriBaka » Mon Apr 22, 2013 8:15 pm

just recently, I made a custom displayable that checks the current mouse position, then places an image there based on a supplied list. combined with setting the renpy mouse cursor to an empty image, this can be used to change the mouse cursor whenever you feel like it.

to use the displayable either show it using

Code: Select all

show expression custom_cursor(cursorlist)
or if it's part of a screen, use

Code: Select all

add custom_cursor(cursorlist)
cursorlist should contain lists (or tuples, I don't really know the difference) of the format [image, area, offset], with area being set to "default" for the default cursor. area should be a rect of the same type as used for mouseareas. if this seems strange, it's because I'm a strange person.

an example would look like this:

Code: Select all

cursorlist = [["default_cursor.png", "default", (0, 0)],
    ["eye_cursor", (50, 50, 50, 50), (0, 0)]]
something to note if you want to get rid of the renpy mouse cursor, I don't know if it was just me screwing up, but when I tried doing it myself, it made the cursor disappear in the menus as well. if this happens, just add custom_cursor to the screens for the menus as well.

since I might as well, I'm also leaving in the part of the file that I designed for making a flashlight follow the cursor. if you want to use that function, make a fullscreen image that is either black or in some other way dark and call it "no_flashlight.png" as well as an image that has height and width double that of your game's resolution with an empty circle in the center and call it "flashlight.png". once you've added those, you can set the keyword argument dark to true when using custom_cursor.
Attachments
custom_cursor.rpy
(2.7 KiB) Downloaded 423 times

User avatar
rabcor
Regular
Posts: 81
Joined: Sun Mar 17, 2013 3:07 pm
Projects: None (Need a programmer?)
Organization: Cestarian Games
Contact:

Re: custom mouse cursor code

#2 Post by rabcor » Fri Nov 29, 2013 8:49 pm

Thanks for this, i was planning on doing something like this in a future project, so this will probably come in handy when the time comes :D

One thing i wonder though, shouldn't

Code: Select all

cursorlist = [["default_cursor.png", "default", (0, 0)],
    ["eye_cursor", (50, 50, 50, 50), (0, 0)]]
be like this?

Code: Select all

cursorlist = [["default_cursor.png", "default", (0, 0)], ["eye_cursor", (50, 50, 50, 50), (0, 0)]]

I wonder why to use this rather than config.mouse though. Even if that can change the cursor, if i understand things right this code would still be useful if modified to place images beneath the cursor, and that's something i'll be needing as-well.

Thanks for the flashlight idea too, that's something cool to keep behind the ear, it might make for some cool gameplay if used right.

The difference between lists and tuples is that tuples are immutable (this means they cannot be changed or edited in any way, the only way to edit a tuple is to convert it to a list and then edit the list, tuples are just there for values that are supposed to be permanent and never supposed to be changed throughout the entire code)

one thing that would be cool though (and as a fellow programmer i highly recommend) would be if you'd use comments more in your code, as a general rule if i'm writing something that isn't completely self explanatory (for example dialogue, or "with" effects (like Pause or Dissolve)) i always add a comment to explain what the hell it was i was writing here and how it works in case i need to look at it again at a later date when i've forgotten how exactly it worked. The nice bonus is that it also makes your code more easily readable to others, especially beginners like me ^_^

As for the cursor disappearing in the menus, you could probably edit an entry somewhere in the code to execute a command (to get the cursor back) everytime a menu is opened, and through variables you could probably even restore the cursor state to what it was before opening the menus when the menus are closed.

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: custom mouse cursor code

#3 Post by KimiYoriBaka » Thu Dec 05, 2013 11:58 pm

the only difference between putting the list on one line and putting it on multiple is that I find putting it on multiple line is easier to read. python (and thus renpy) can still tell that it's one list by looking at the brackets.

you're right about the comments though. I tend not to think about that when programming.

User avatar
AlfieMachica123
Regular
Posts: 56
Joined: Sun Jun 30, 2013 10:15 pm
Contact:

Re: custom mouse cursor code

#4 Post by AlfieMachica123 » Sun Dec 15, 2013 11:56 am

hi :)

how do you change a mouse cursor when it hovers a specific imagebutton?

how do you put it here?

Code: Select all

imagebutton:
     idle "dumb1.png"
     hover "dumb2.png"
     xpos 0
     ypos 0
     action SetVariable("dumb3", dumb4)

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: custom mouse cursor code

#5 Post by KimiYoriBaka » Mon Dec 16, 2013 6:44 pm

how do you change a mouse cursor when it hovers a specific imagebutton?
as far as I know, there's no way to have the mouse cursor change based on the imagebutton itself, so you would need to instead tell the cursor to change when it hovers the area the imagebutton happens to be in.

something like this:

Code: Select all

screen dumb_screen:
    imagebutton:
        idle "dumb1.png"
        hover "dumb2.png"
        xpos 0
        ypos 0
        action SetVariable("dumb3", dumb4)

    add custom_cursor(cursorlist)

label start:
    $ cursorlist = [["default_cursor.png", "default", (0, 0)],
        ["dumb_cursor.png", (0, 0, dumb_width, dumb_height), (0, 0)]]
    show screen dumb_screen
where dumb_width and dumb_height are the width and height of "dumb1.png". if the screen has more imagebuttons, you would just add more to cursorlist. there is no problem with having multiple entries with the same cursor.

User avatar
AlfieMachica123
Regular
Posts: 56
Joined: Sun Jun 30, 2013 10:15 pm
Contact:

Re: custom mouse cursor code

#6 Post by AlfieMachica123 » Wed Dec 18, 2013 5:51 am

hoho, thank you so much, this is very helpful :) again, thank you

User avatar
chocoberrie
Veteran
Posts: 254
Joined: Wed Jun 19, 2013 10:34 pm
Projects: Marshmallow Days
Contact:

Re: custom mouse cursor code

#7 Post by chocoberrie » Thu May 22, 2014 1:52 pm

KimiYoriBaka wrote:to use the displayable either show it using

Code: Select all

show expression custom_cursor(cursorlist)
or if it's part of a screen, use

Code: Select all

add custom_cursor(cursorlist)
an example would look like this:

Code: Select all

cursorlist = [["default_cursor.png", "default", (0, 0)],
    ["eye_cursor", (50, 50, 50, 50), (0, 0)]]
Hello!

I tried using the code you provided to show a custom cursor, and I got the "expected statement" error. If you could help me out, that would be much appreciated!

Here's my code:

Code: Select all

    init:
        add custom_cursor(cursorlist)        
        cursorlist = [["GUI/light-red-cursor.png", "default", (0, 0)], ["eye_cursor", (50, 50, 50, 50), (0, 0)]]
I tried putting a colon after (cursorlist), but that didn't work. :(

Thanks again!

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: custom mouse cursor code

#8 Post by KimiYoriBaka » Sun May 25, 2014 6:54 am

I got the "expected statement" error.
I think that's because the "add" statement is specific to screen language. I'm not sure what you're trying to do, but it isn't a good idea to use this code during an init block. The point behind this code is to be able to change the cursor during the game, not before it. I designed this for use in point-and-click games.

If just want to change the mouse cursor, changing config.mouse would work better. That would look like this:

Code: Select all

init:
    config.mouse = {"default" : ("GUI/light-red-cursor.png", 0, 0)}
If you want to change the mouse cursor to fit what's going on in your story, but you don't need the mouse to check what part of the screen it's in, then you can just hand the custom cursor code a new cursor at beginning of each scene in which it changes.

Code: Select all

label next_scene:
    $ cursorlist = [["GUI/light-red-cursor.png", "default", (0, 0)]]
    show expression custom_cursor(cursorlist)
note: you don't need to use the "show expression" statement if you haven't cleared the screen

User avatar
chocoberrie
Veteran
Posts: 254
Joined: Wed Jun 19, 2013 10:34 pm
Projects: Marshmallow Days
Contact:

Re: custom mouse cursor code

#9 Post by chocoberrie » Sun May 25, 2014 11:01 am

KimiYoriBaka wrote:If just want to change the mouse cursor, changing config.mouse would work better. That would look like this:

Code: Select all

init:
    config.mouse = {"default" : ("GUI/light-red-cursor.png", 0, 0)}
That's exactly what I want! But where to I put that code? I tried putting it at the end of options.rpy, but I got a traceback error.

Thanks so much for your help! :)

User avatar
KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: custom mouse cursor code

#10 Post by KimiYoriBaka » Mon May 26, 2014 12:10 am

Oh, whoops. That was my fault. The line for changing config.mouse should be a python statement:

Code: Select all

init:
    $ config.mouse = {"default" : ("GUI/light-red-cursor.png", 0, 0)}
hopefully I didn't forget anything else...

User avatar
chocoberrie
Veteran
Posts: 254
Joined: Wed Jun 19, 2013 10:34 pm
Projects: Marshmallow Days
Contact:

Re: custom mouse cursor code

#11 Post by chocoberrie » Mon May 26, 2014 3:04 pm

KimiYoriBaka wrote:

Code: Select all

init:
    $ config.mouse = {"default" : ("GUI/light-red-cursor.png", 0, 0)}
That didn't work either; I got another traceback error. But no worries! I found a code in another thread that worked. Thank you for your help! :)

User avatar
Ben Ayoo
Regular
Posts: 60
Joined: Mon Feb 24, 2014 1:08 am
Completed: The coin and the gun
Organization: Ben Studio
Location: In a house
Contact:

Re: custom mouse cursor code

#12 Post by Ben Ayoo » Mon Aug 04, 2014 1:12 pm

So confusing. :?
If you want to change the mouse cursor to fit what's going on in your story, but you don't need the mouse to check what part of the screen it's in, then you can just hand the custom cursor code a new cursor at beginning of each scene in which it changes.
What do you put on the options.rpy? This?

Code: Select all

    $  config.mouse = {'default' :[('mouse1.png', 0, 0)]}
    $  config.mouse = {'default' :[('mouse2.png', 0, 0)]}

Then put this code for every time I want to change the mouse cursor?

Code: Select all

init:
    $ config.mouse = {'default' : ('mouse2.png', 0, 0)}
Anything I am doing wrong? Looking for assistance.

Sorry I am total beginner to programming.

DoctorScaglietti
Newbie
Posts: 8
Joined: Tue Dec 31, 2013 10:36 pm
Projects: Heartbleed Wonderland
Location: Right behind you
Contact:

Re: custom mouse cursor code

#13 Post by DoctorScaglietti » Tue Aug 19, 2014 1:49 pm

I'm planning on using this with some imagebuttons as an alternative to the hover state, but I don't know how to set the cursor-changing areas to the image itself. The thing about imagebuttons is that they don't use the transparent parts of the image if you're using focus_mask, so it's possible to use the fine edges of the image rather than unintuitive rectangles. Is there any way to make it so it automatically sets a cursorlist entry to a certain image at the position of choice? The image will already be there in that position, I'd just like to not have to drag a million rectangles over a screenshot of the combined background and imagebutton in order to get coordinates. It's a ridiculously roundabout way and I was wondering if there was any alternative.

User avatar
Alex
Lemma-Class Veteran
Posts: 2349
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: custom mouse cursor code

#14 Post by Alex » Tue Aug 19, 2014 4:38 pm

You can set mouse property for your buttons, so pointer will change its appearence while hovering them.
http://www.renpy.org/doc/html/style_pro ... properties

Post Reply

Who is online

Users browsing this forum: No registered users