{"id":850,"date":"2022-11-23T10:07:30","date_gmt":"2022-11-23T09:07:30","guid":{"rendered":"https:\/\/betalord.com\/code\/?p=850"},"modified":"2023-04-05T19:57:28","modified_gmt":"2023-04-05T17:57:28","slug":"libgdx-mouse-cursor","status":"publish","type":"post","link":"https:\/\/betalord.com\/code\/2022\/11\/23\/libgdx-mouse-cursor\/","title":{"rendered":"Mouse cursor"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"850\" class=\"elementor elementor-850\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-97dbafd elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"97dbafd\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3cc82cf\" data-id=\"3cc82cf\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-d42fea1 elementor-widget elementor-widget-text-editor\" data-id=\"d42fea1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Mouse cursor that we load on one monitor may look very small on higher DPI monitors, that is why we should load different sizes of mouse cursors and apply the one that fits best the monitor&#8217;s DPI.<\/p><p>Note that in order to simulate different DPIs, we can simply change monitor&#8217;s scale in Windows&#8217; display settings (under &#8220;Scale and layout&#8221;).<\/p><p>Here is a little class that offers basic functionality to solve given problem.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-b0f19d3 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"b0f19d3\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b501fe6\" data-id=\"b501fe6\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-20fe3cb elementor-widget elementor-widget-code-block-for-elementor\" data-id=\"20fe3cb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-block-for-elementor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<pre class='line-numbers theme-okaidia' data-show-toolbar='yes'><code class='language-java'>package com.betalord.sgx.core;\n\nimport java.awt.Toolkit;\n\nimport com.badlogic.gdx.Gdx;\nimport com.badlogic.gdx.graphics.Pixmap;\nimport com.badlogic.gdx.graphics.Texture;\nimport com.badlogic.gdx.graphics.TextureData;\nimport com.badlogic.gdx.graphics.Pixmap.Format;\nimport com.badlogic.gdx.graphics.g2d.TextureRegion;\nimport com.badlogic.gdx.graphics.glutils.PixmapTextureData;\nimport com.badlogic.gdx.scenes.scene2d.utils.Drawable;\nimport com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;\nimport com.badlogic.gdx.utils.Disposable;\n\n\/**\n * Class that contains all of the logic to load and set mouse cursor image.\n * \n * @author Betalord\n *\/\n public class MouseCursor {\n\t\/**\n\t * Note: we don&#039;t statically load cursor data since we must not do that on Android.\n\t * This is why we load it on {@link MouseCursor#init()}, which should get invoked on desktop only.\n\t *\/\n\tpublic static enum MouseCursorSize {\n\t\t\/** 24x24 pixels *\/\n\t\tP24(24),\n\t\t\/** 32x32 pixels *\/\n\t\tP32(32),\n\t\t\/** 40x40 pixels *\/\n\t\tP40(40),\n\t\t\/** 48x48 pixels *\/\n\t\tP48(48),\n\t\t\/** 56x56 pixels *\/\n\t\tP56(56),\n\t\t\/** 64x64 pixels *\/\n\t\tP64(64);\n\t\t\n\t\t\/** Size in pixels (n x n pixels). Note that cursor image itself is padded to power of two sized image, however the cursor image itself may be smaller. *\/\n\t\tprivate int pixels;\n\t\tprivate MouseCursorData data;\n\t\t\n\t\tMouseCursorSize(int pixels) {\n\t\t\tthis.pixels = pixels;\n\t\t}\n\t\t\n\t\tpublic int getPixels() {\n\t\t\treturn pixels;\n\t\t}\n\t\t\n\t\tpublic MouseCursorData getData() {\n\t\t\treturn data;\n\t\t}\n\t\t\n\t\t@Override\n\t\tpublic String toString() {\n\t\t\treturn pixels + &quot; x &quot; + pixels;\n\t\t}\n\t\t\n\t\tpublic static MouseCursorSize getClosest(int pixels) {\n\t\t\tMouseCursorSize value = null;\n\t\t\tint diff = Integer.MAX_VALUE;\n\t\t\tfor (MouseCursorSize size : MouseCursorSize.values())\n\t\t\t\tif (Math.abs(pixels-size.pixels) &lt;= diff) {\n\t\t\t\t\tdiff = Math.abs(pixels-size.pixels);\n\t\t\t\t\tvalue = size;\n\t\t\t\t}\n\t\t\treturn value;\n\t\t}\n\t\t\n\t\tprivate MouseCursorData load() {\n\t\t\tMouseCursorData data = new MouseCursorData();\n\t\t\t\n\t\t\t\/\/ create a 1x1 white pixel texture:\n\t\t\tdata.pixmap = new Pixmap(Gdx.files.internal(&quot;gfx\/mouse_&quot; + pixels + &quot;.png&quot;));\n\t\t\tTextureData texData = new PixmapTextureData(data.pixmap, Format.RGBA8888, false, false, false);\n\t\t\tdata.texture = new Texture(texData);\n\t\t\tdata.drawable = new TextureRegionDrawable(new TextureRegion(data.texture));\n\t\t\t\n\t\t\treturn data;\n\t\t}\n\t}\n\n\tpublic static class MouseCursorData implements Disposable {\n\t\tpublic Pixmap pixmap;\n\t\tpublic Texture texture;\n\t\tpublic Drawable drawable;\n\t\t\n\t\t@Override\n\t\tpublic void dispose() {\n\t\t\tpixmap.dispose();\n\t\t\ttexture.dispose();\n\t\t}\n\t}\n\t\n\t\/**\n\t * Loads cursors of all sizes and creates textures for them.\n\t * Should be called on desktop only (Android doesn&#039;t have mouse and hence does not use mouse cursors).\n\t *\/\n\tpublic static void init() {\n\t\tfor (MouseCursorSize size : MouseCursorSize.values())\n\t\t\tsize.data = size.load();\n\t}\n\t\n\t\/**\n\t * Will determine best mouse cursor image size (based on monitor&#039;s PPI value).\n\t *\/\n\tpublic static MouseCursorSize determineClosest() {\n\t\tfloat ppi = Toolkit.getDefaultToolkit().getScreenResolution();\n\t\tfloat start = 96; \/\/ PPI value for which we use the smallest cursor, 24x24 pixels\n\t\tint pixels = Math.round(24 * (ppi\/start));\n\t\treturn MouseCursorSize.getClosest(pixels);\n\t}\n}\n<\/code><\/pre>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Mouse cursor that we load on one monitor may look very small on higher DPI monitors, that is why we should load [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[10],"class_list":["post-850","post","type-post","status-publish","format-standard","hentry","category-article","tag-libgdx"],"_links":{"self":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/comments?post=850"}],"version-history":[{"count":5,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":867,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/posts\/850\/revisions\/867"}],"wp:attachment":[{"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/media?parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/categories?post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/betalord.com\/code\/wp-json\/wp\/v2\/tags?post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}