bl_info = {
    "name": "Grid Scale Keys",
    "author": "Amandeep",
    "description": "Change Viewport Grid Scale with (-/+)",
    "blender": (2, 90, 0),
    "version": (1, 0, 0),
    "warning": "",
    "category": "Object",
}

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import bpy
import os
import rna_keymap_ui
#print(os.path.splitext(os.path.basename(__file__))[0])
def myround(x, prec=2, base=.05):
    return round(base * round((float(x)/base)), prec)
def preferences():
    return bpy.context.preferences.addons[os.path.splitext(os.path.basename(__file__))[0]].preferences
kmaps_3dview=["gs.changescale"]
def draw_hotkeys(col,km_name):
    kc = bpy.context.window_manager.keyconfigs.user
    for kmi in kmaps_3dview:
        km2=kc.keymaps[km_name]
        kmi2=[]
        for a,b in km2.keymap_items.items():
            if a==kmi:
                kmi2.append(b)
        kmi2=kmi2[:-1]
        if kmi2:
            for a in kmi2:
                col.context_pointer_set("keymap", km2)
                rna_keymap_ui.draw_kmi([], kc, km2, a, col, 0)
class GSPrefs(bpy.types.AddonPreferences):
    bl_idname = os.path.splitext(os.path.basename(__file__))[0]
    increment:bpy.props.FloatProperty(default=0.1,name="Increment")
    def draw(self,context):
        layout=self.layout
        draw_hotkeys(layout,'3D View')
        layout.prop(self,'increment')
class GS_OT_Scale(bpy.types.Operator):
    bl_idname = "gs.changescale"
    bl_label = "Change Scale"
    bl_description = "Increase/Decrease Grid Size"
    bl_options = {"REGISTER","UNDO"}
    increment:bpy.props.BoolProperty(default=True,options={'SKIP_SAVE','HIDDEN'})
    def execute(self,context):
        bpy.context.space_data.overlay.grid_scale +=(preferences().increment if self.increment else -preferences().increment)
        return {'FINISHED'}

classes = (GS_OT_Scale,GSPrefs
)

icon_collection={}
addon_keymaps = []

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)
    
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    kmaps = [
    ]

    km = kc.keymaps.new(name="3D View", space_type="VIEW_3D")
    if kc:
        kmi = km.keymap_items.new(
            "gs.changescale", type="EQUAL", value="PRESS",repeat=True
        )
        kmi.properties.increment=True
        addon_keymaps.append((km, kmi))
        kmi = km.keymap_items.new(
            "gs.changescale", type="MINUS", value="PRESS",repeat=True
        )
        
        kmi.properties.increment=False
        addon_keymaps.append((km, kmi))
def unregister():

    from bpy.utils import unregister_class

    for cls in reversed(classes):
        unregister_class(cls)
    for (km, kmi) in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()
if __name__ == "__main__":
    register()

